package com.blockmeta.bbs.overallserviceapplication.search.fragment;


import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blockmeta.bbs.baselibrary.net.callback.MyStringCallback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.pojo.HomeMarketItemPOJO;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.R2;
import com.blockmeta.bbs.overallserviceapplication.search.adapter.HomeMarketAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Arrays;

import butterknife.BindView;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExchangeFragment extends BaseFragment {


    @BindView(R2.id.rcv_exchange_list)
    RecyclerView mRcvExchangeList;

    private HomeMarketAdapter mAdapter;
    private String mKey;

    public static ExchangeFragment instance(String key){
        ExchangeFragment exchangeFragment = new ExchangeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key",key);
        exchangeFragment.setArguments(bundle);
        return exchangeFragment;
    }
    @Override
    public View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_exchange, container, false);
    }

    @Override
    public void initView() {
        mRcvExchangeList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new HomeMarketAdapter(R.layout.item_home_market,null,mContext);
        mRcvExchangeList.setAdapter(mAdapter);
        mKey = getArguments().getString("key");
        netWork();
    }

    private void netWork() {
        String url = NetConstant.SEARCH_EXCHANGE+"?key="+mKey;
        OkHttpUtils.get()
                .addHeader("token", SharedPreferenceUtil.getString(mContext, Constant.TOKEN,null))
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_search,null));
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            HomeMarketItemPOJO[] homeMarketItemPOJOS = new Gson().fromJson(response,HomeMarketItemPOJO[].class);
                            if (homeMarketItemPOJOS==null||homeMarketItemPOJOS.length == 0) {
                                mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_search,null));
                            }else {
                                mAdapter.setNewData(Arrays.asList(homeMarketItemPOJOS));
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_search,null));
                        }

                    }
                });
    }
}
